<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/4
 * Time: 18:25
 */
namespace app\index\controller;

use think\Controller;
use think\Db;

class Auth extends Controller {
    //用户授权获取code
    public function index()
    {
        //授权完跳转的网址
        $path = $_REQUEST['path'];
        $student_id = $this->request->get('student_id');
        //用户同意授权后回调的网址.必须使用url对回调网址进行编码，我们也将授权完跳转对网址,
        $redirect_uri = urlencode('http://' . $_SERVER['HTTP_HOST'] . '/index/Auth/callback?student_id='.$student_id);
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd2be405bb44f5112&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=' . $path . '#wechat_redirect';
        $this->redirect($url);
    }
    //获取code后的回调函数
    public function callBack()
    {
        //获取到的code
        $code = $_REQUEST['code'];
        $student_id = $this->request->get('student_id');
        //授权结束后的回调网址
        $path = $_REQUEST['state'];

        //获取access_token
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd2be405bb44f5112&secret=ee3f94ea128217b8c09ecf832e55b340&code=' . $code . '&grant_type=authorization_code';
        $data = $this->sendRequest($url);


        //如果获取成功，根据access_token和openid获取用户的基本信息
        if ($data != null && $data['access_token']) {

            //获取用户的基本信息，并将用户的唯一标识保存在session中


            $user_data = $this->sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN');
            
            //dump($user_data);die;
            if ($user_data != null && $user_data['openid']) {

                //将用户信息存在数据库中,同时将用户在数据库中唯一的标识保存在session中
                $array = [];

                $array['openid'] = $user_data['openid'];
                $array['nick_name'] = $user_data['nickname'];
                $array['image'] = $user_data['headimgurl'];
                $array['section_id'] = $student_id;
                $array['createtime'] = time();

                //我这里只存储了用户的openid,nickname,headimgurl
                //$model = M('user');
                //先判断用户数据是不是已经存储了，如果存储了获取用户在数据库中的唯一标识
                $user_id = Db::name('people')->where(['openid' => $array['openid']])->find();
                if ($user_id) {
                    session('user_id', $user_id['id']);
                    $this->redirect($path);
                } else {
                    Db::name('people')->insert($array);
                    $user_id = Db::name('people')->getLastInsID();
                    //将用户在数据库中的唯一表示保存在session中
                    session('user_id', $user_id);
                    echo "<div style='position:fixed;bottom:100px;left: 25%'><img src='/uploads/qrcode/15676079484280578001.jpg'></div>

                    <div style='position:fixed;bottom:500px;left: 30%;font-size: 50px;color: #00FF00'>长按识别二维码</div>";
                }
                //跳转网页

                $url = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzAwMjI5MDgyMA==';
                //echo "<span>https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzAwMjI5MDgyMA==</span>";
                //header("location:https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzAwMjI5MDgyMA==");

                //header('Location:' . $url);
            } else {



                exit('获取用户信息失败！');

            }
        } else {

            curl_close($curl);

            exit('微信授权失败');
        }

    }
    public function sendRequest($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
}